package com.elinshaw.netty4.config

import com.elinshaw.protobuf.UserMessage
import io.netty.channel.ChannelHandler
import io.netty.handler.codec.protobuf.ProtobufDecoder
import io.netty.handler.codec.protobuf.ProtobufEncoder
import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender
import io.netty.handler.timeout.IdleStateHandler

import java.util.concurrent.TimeUnit

/**
 * @author Administrator
 */
class NettyClient extends NettyClientTemplate {

    private int port
    private String address

    NettyClient(def port, def address) {
        this.port = port
        this.address = address
    }

    @Override
    protected ChannelHandler[] createHandlers() {
        return [new IdleStateHandler(0, 4, 0, TimeUnit.SECONDS),
                /* new StringDecoder(Charset.forName("utf-8")),
                 new StringEncoder(Charset.forName("utf-8")),*/
                new ProtobufDecoder(UserMessage.User.getDefaultInstance()),
                new ProtobufVarint32LengthFieldPrepender(),
                new ProtobufEncoder(),
                new ClientHandler(),
                new HeartBeatClientHandler()] as ChannelHandler[]
    }

    @Override
    int getPort() {
        return port
    }

    @Override
    String getAddress() {
        return address
    }

}
